/*
 * @Descripttion:
 * @Author: YuZhenJing
 * @Date: 2020-01-10 20:09:42
 * @LastEditors  : YuZhenJing
 * @LastEditTime : 2020-02-12 16:07:05
 */

/**
 * 查询组织机构状态数据集
 */
export function searchOrgStatusOptions() {
  this.treeLoading = true
  const options = this.$html.get('org/orgStatusOptions')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.orgStatusOptions = this.$objTools.dicToArray(data.returnData)
        this.orgTreeData()
      }
    })
    .catch(() => {
      this.treeLoading = false
      console.log('error:searchOrgStatusOptions')
    })
}

/**
 * 获取部门树
 */
export function getOrgTreeData() {
  const options = this.$html.get('org/treeData')
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.orgTreeData = data.returnData
      }
    })
    .catch(() => {
      console.log('error:getOrgTreeData')
    })
}

/**
 * 加载组织机构数据集
 */
export function orgTreeData() {
  this.treeLoading = true
  let param = {
    orgStatus: this.orgStatus
  }
  const options = this.$html.get('org/orgTreeData', param)
  options
    .then(data => {
      if (data.returnType == 'success') {
        this.orgData = [
          {
            orgCode: '99999999999',
            orgName: '组织机构',
            isRoot: true,
            children: data.returnData
          }
        ]
        this.changeStatus()
        this.treeLoading = false
      }
    })
    .catch(() => {
      console.log('error:org/orgTreeData')
      this.treeLoading = false
    })
}

/**
 * 修改状态
 */
export function changeStatus() {
  this.$refs.orgTree.filter(this.filterText)
}

/**
 * 删除组织机构
 */
export function delOrg(rowObj) {
  if (rowObj.children && rowObj.children.length != 0) {
    this.$alert('当前节点存在子节点,无法删除', '提示', {
      type: 'warning',
      confirmButtonText: '确定'
    })
    return
  }
  this.$confirm('确认要删除【' + rowObj.orgName + '】吗?', '提示信息', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  })
    .then(() => {
      this.treeLoading = true
      const delResult = this.$html.delete('org/delOrg', rowObj.orgCode)
      delResult
        .then(data => {
          if (data.returnType == 'success') {
            this.$notify({
              title: '成功',
              message: '删除成功',
              type: 'success'
            })
            this.orgTreeData()
          }
        })
        .catch(() => {
          this.treeLoading = false
          console.log('error:org/delOrg')
        })
    })
    .catch(() => {
      this.$notify.info({
        title: '消息',
        message: '已取消删除'
      })
    })
}

/**
 * 更新组织机构状态
 */
export function updateStatus(status, data, parentData) {
  if (!this.whetherCanUpdate(status, parentData.orgStatus)) {
    return false
  }
  let statusName = status == 'start' ? '启用' : '停用'
  this.$confirm(
    '确认要' + statusName + '【' + data.orgName + '】吗?',
    '提示信息',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  )
    .then(() => {
      let findOrgParentCode = (itemObj, parentCodeSet) => {
        if (itemObj.children && itemObj.children.length != 0) {
          itemObj.children.map(item => {
            parentCodeSet.add(item.parentLevelCode)
            findOrgParentCode(item, parentCodeSet)
          })
        }
      }
      let parentCodes = new Set()
      findOrgParentCode(data, parentCodes)
      let param = {
        orgCode: data.orgCode,
        orgStatus: status,
        parentCodes: [...parentCodes]
      }
      this.doUpdateStatus(param)
    })
    .catch(() => {
      this.$notify.info({
        title: '消息',
        message: '已取消删除'
      })
    })
}

/**
 * 是否可以更新
 */
export function whetherCanUpdate(status, orgStatus) {
  let refusalUpdate = true
  let refusalMsg = ''
  if (status == 'start') {
    if (orgStatus == 'start') {
      refusalUpdate = false
      refusalMsg = ''
    } else {
      refusalUpdate = true
      refusalMsg = '启用当前机构时父级必须为【启用】状态'
    }
  } else if (status == 'stop') {
    if (orgStatus != 'designing') {
      refusalUpdate = false
      refusalMsg = ''
    } else {
      refusalUpdate = true
      refusalMsg = '停用当前机构时父级不能为【设计中】状态'
    }
  }
  if (refusalUpdate) {
    this.$alert(refusalMsg, '提示', {
      type: 'warning',
      confirmButtonText: '确定'
    })
    return false
  } else {
    return true
  }
}

/**
 * 执行更新状态
 */
export function doUpdateStatus(param) {
  this.treeLoading = true
  const updateResult = this.$html.putJson('org/updateStatusBatch', param)
  updateResult
    .then(data => {
      if (data.returnType == 'success') {
        this.$notify({
          title: '成功',
          message: '更新成功',
          type: 'success'
        })
        this.orgTreeData()
      }
    })
    .catch(() => {
      this.treeLoading = false
      console.log('error:org/doUpdateStatus')
    })
}
